package mytools;

import android.app.DownloadManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import java.util.Date;

import broadcastreceiver.DownLoadReceiver;
import dialog.ProgressDialogFragment;
import ui.LoginActivity;

/**
 * Created by Monkey on 2017/10/23.
 * 自定义基Activity
 */
public class MyBaseActivity extends AppCompatActivity {

    private ProgressDialogFragment progressDialogFragment;

    private ApplicationUtil myApplication;
    private SharedPreferences mySharedPreferences;

    private DownLoadReceiver downLoadReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myApplication = (ApplicationUtil) getApplication();
        mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        downLoadReceiver = new DownLoadReceiver();
        IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        this.registerReceiver(downLoadReceiver, intentFilter);
//        uploadListener = (currentStep, uploadSize, file) -> {
//            Log.d("APE", currentStep);
//            switch (currentStep) {
//                case FtpUtil.FTP_UPLOAD_SUCCESS:
//                    KLog.d("APE", file.getName() + FtpUtil.FTP_UPLOAD_SUCCESS);
////                    Toast.makeText(getApplicationContext(), file.getName() + " 上传完成", Toast.LENGTH_SHORT).show();
//                    break;
//                case FtpUtil.FTP_UPLOAD_LOADING:
//                    long fize = file.length();
//                    float num = (float) uploadSize / (float) fize;
//                    int result = (int) (num * 100);
//                    KLog.d("APE", FtpUtil.FTP_UPLOAD_LOADING + ":" + result);
//                    break;
//                case FtpUtil.FTP_DISCONNECT_SUCCESS:
//                    stopUploadThread();
//                    break;
//                case FtpUtil.FTP_CONNECT_FAIL:
//                    KLog.d("APE", FtpUtil.FTP_CONNECT_FAIL);
//                    break;
//                case FtpUtil.FTP_UPLOAD_FAIL:
//                    KLog.d("APE", FtpUtil.FTP_UPLOAD_FAIL);
//                    break;
//            }
//        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(downLoadReceiver);
        System.gc();
    }

    public ApplicationUtil getMyApplication() {
        return myApplication;
    }

    public SharedPreferences getMySharedPreferences() {
        return mySharedPreferences;
    }

    public void cancelProgressDialog() {
        if (progressDialogFragment != null && progressDialogFragment.getDialog() != null) {
            progressDialogFragment.getDialog().dismiss();
        }
    }

    public void showProgressDialog(String message) {
        cancelProgressDialog();
        progressDialogFragment = new ProgressDialogFragment();
        progressDialogFragment.setMessage(message);
        progressDialogFragment.show(getFragmentManager(), "downLoading");

    }

//    private void stopUploadThread() {
//        isUploading = false;
//        if (ftpThread != null && ftpThread.isAlive()) {
//            ftpThread.interrupt();
//            ftpThread = null;
//        }
//    }

//    private LinkedList<File> doGetUploadFiles() {
//        String logPath = Environment.getExternalStorageDirectory().getAbsolutePath()
//                + File.separator + "crash" + File.separator;
//        File crashDir = new File(logPath);
//        Date deleDate;
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE) - 1);
//        String deleteDateString = DateUtil.getDFFormatString(calendar.getTime());
//        deleDate = DateUtil.getDFormatDate(deleteDateString);
//        if (deleDate == null) {
//            deleDate = new Date();
//        }
//        if (crashDir.isDirectory()) {
//            File[] files = crashDir.listFiles();
//            LinkedList<File> fileList = new LinkedList<>();
//            for (File file : files
//                    ) {
//                String fileName = file.getName();
//                if (fileName.contains(".log") && fileName.startsWith("wtvs-")) {
//                    String fileDateString = fileName.substring(5, 15);
//                    Date fileDate = DateUtil.getDFormatDate(fileDateString);
//                    if (fileDate != null && fileDate.getTime() < deleDate.getTime()) {
//                        fileList.add(file);
//                    }
//                }
//            }
//            KLog.d("size:" + fileList.size());
//            return fileList;
//        }
//        return null;
//    }

//    public void doUploadLogFile() {
//        final LinkedList<File> fileList = doGetUploadFiles();
//        if (fileList != null && fileList.size() > 0) {
//            if (!isUploading) {
//                isUploading = true;
//                ftpThread = new Thread(() -> {
//                    while (isUploading) {
//                        try {
//                            new FtpUtil().uploadMultiFile(fileList, uploadListener, true);
//                        } catch (IOException e) {
//                            stopUploadThread();
//                            KLog.d("APE", "error: " + e.getMessage());
//                            e.printStackTrace();
//                        }
//                    }
//                });
//                ftpThread.start();
//            }
//        } else {
//            Toast.makeText(getApplicationContext(), "没有需要上传的日志！", Toast.LENGTH_SHORT).show();
//        }
//    }

    public void doBackToHome() {
        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(intent);
        finish();
    }

    protected String getTodayDate() {
        return DateUtil.getDFFormatString(new Date());
    }
}
